package leetcode.offer_two_special.string;

/**
 * @author mazouri
 * @create 2021-09-05 23:02
 */
public class _14_checkInclusion {
    /**
     *
     * @param s1 子串
     * @param s2  大串
     * @return  有子串返回true
     */
    public boolean checkInclusion(String s1, String s2) {
        if (s2.length()<s1.length()) return false;

        int[] letters = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            letters[s1.charAt(i)-'a']++;
            //将初始的设置为-1
            letters[s2.charAt(i)-'a']--;
        }

        if (allZero(letters)) return true;

        for (int i = s1.length(); i < s2.length(); i++) {
            letters[s2.charAt(i)-'a']--;
            letters[s2.charAt(i-s1.length())-'a']++;

            if (allZero(letters)) return true;
        }

        return false;
    }

    private boolean allZero(int[] letters) {
        for (int letter : letters) {
            if (letter != 0 )  return false;
        }
        return true;
    }
}
